package com.antkit.mark.ak.wedigt;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.ColorInt;
import android.util.AttributeSet;
import android.view.SurfaceView;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Mark
 * @Date on 2019/3/5
 **/
public class ZuanShiView extends View {
    public ZuanShiView(Context context) {
        super(context);
    }

    public ZuanShiView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ZuanShiView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ZuanShiView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private Paint mPaint = new Paint();

    private List<Integer> colorList = new ArrayList<>();

    public void setColorList(List<Integer> colorList) {
        this.colorList = colorList;
    }

    public void setW(int w) {
        this.w = w;
    }

    public void setH(int h) {
        this.h = h;
    }

    private int w,h;

    private int minPix = 9;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        for (int i = 0; i <h ; i++) {

            for (int j = 0; j <w ; j++) {
                int x = j*minPix;
                int y = i *minPix;
                mPaint.setAntiAlias(true);
                mPaint.setColor(colorList.get(i*w+j));
                canvas.drawCircle(x,y,minPix/2,mPaint);
                drawStar(x,y,canvas,mPaint,Color.WHITE,minPix/2,7,true);
            }
        }
/*        for (int i = 0; i <colorList.size() ; i++) {
            mPaint.setColor(colorList.get(i));
            canvas.drawCircle(getHeight()/w,getHeight()/w,getHeight()/w,mPaint);
            drawStar(canvas,mPaint,Color.WHITE,getHeight()/w,7,true);

            int x = i*8%w;
            int y;
            if (i%w==0&&i!=0){
                y = 8*(i/w - 1);
            }else{
                y = 8*(i/w);
            }
            canvas.translate(x,y);

        }*/
      }

    /**
     * 绘制彩色多边形或星形
     * @param canvas Canvas画布
     * @param paint Paint画笔
     * @param color 颜色
     * @param radius 外接圆半径
     * @param count 外顶点数
     * @param isStar 是否为星形
     */
    private void drawStar(int x,int y,Canvas canvas, Paint paint, @ColorInt int color, float radius, int count, boolean isStar){
        canvas.translate(x,y);
        if ((!isStar) && count < 3){
            canvas.translate(-radius,-radius);
            return;
        }
        if (isStar && count < 5){
            canvas.translate(-radius,-radius);
            return;
        }
        canvas.rotate(-90);
        if (paint == null){
            paint = new Paint();
        }else{
            paint.reset();
        }
        Path path = new Path();
        float inerRadius = count%2==0?
                (radius*(cos(360/count/2)-sin(360/count/2)*sin(90-360/count)/cos(90-360/count)))
                :(radius*sin(360/count/4)/sin(180-360/count/2-360/count/4));
        for (int i=0;i<count;i++){
            if (i==0){
                path.moveTo(radius*cos(360/count*i),radius*sin(360/count*i));
            }else{
                path.lineTo(radius*cos(360/count*i),radius*sin(360/count*i));
            }
            if (isStar){
                path.lineTo(inerRadius*cos(360/count*i+360/count/2),inerRadius*sin(360/count*i+360/count/2));
            }
        }
        path.close();
        paint.setColor(color);
        canvas.drawPath(path,paint);
        canvas.rotate(90);
        canvas.translate(-x,-y);
    }

    /**
     * Math.sin的参数为弧度，使用起来不方便，重新封装一个根据角度求sin的方法
     * @param num 角度
     * @return
     */
    float sin(int num){
        return (float) Math.sin(num*Math.PI/180);
    }

    /**
     * 与sin同理
     */
    float cos(int num){
        return (float) Math.cos(num*Math.PI/180);
    }
}
